import { isValid } from 'cc';
import { v4 } from 'cc';
import { Sprite } from 'cc';
import { _decorator, Component, Node } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('itemmatch_rotshine')
export class itemmatch_rotshine extends Component {

    _sprite: Sprite = null;

    @property
    useMaterialInstance: boolean = false;

    onLoad() {
        this._sprite = this.getComponent(Sprite);

        // this.scheduleOnce(() => {
        //     if (isValid(this.node)) {
        //         this.refreshMaterial();
        //     }
        // }, .1);
    }

    @property
    private _width1: number = .25;
    @property({ slide: true, range: [0, 1, .01] })
    public get width1(): number { return this._width1; }
    public set width1(v: number) {
        this._width1 = v;
        this.refreshMaterial();
    }

    @property
    private _width2: number = .1;
    @property({ slide: true, range: [0, 1, .01] })
    public get width2(): number { return this._width2; }
    public set width2(v: number) {
        this._width2 = v;
        this.refreshMaterial();
    }

    @property
    private _opacity: number = .4;
    @property({ slide: true, range: [0, 1, .01] })
    public get opacity(): number { return this._opacity; }
    public set opacity(v: number) {
        this._opacity = v;
        this.refreshMaterial();
    }

    @property
    private _t: number = .4;
    @property({ slide: true, range: [-6.30, 6.30, .01] })
    public get t(): number { return this._t; }
    public set t(v: number) {
        this._t = v;
        this.refreshMaterial();
    }

    refreshMaterial() {
        if (this._sprite == null) this._sprite = this.node.getComponent(Sprite);
        if (this._sprite == null || this._sprite.spriteFrame == null) return;

        let material = this.useMaterialInstance ? this._sprite.getMaterialInstance(0) : this._sprite.getSharedMaterial(0);
        if (material) {
            material.setProperty('arg0', v4(this.t, this.opacity, this.width1, this.width2));

            this.useMaterialInstance ? this._sprite.setMaterialInstance(material, 0) : this._sprite.setSharedMaterial(material, 0);
        }

    }
}

